package com.seleniti.enpareja.web.appbase.client.control.controller;

import org.ioc.commons.flowcontrol.messageboxfactory.MessageBoxFactory;
import org.ioc.commons.ui.features.HasInfoType.InfoType;
import org.ioc.commons.utils.Format;

import com.google.gwt.user.client.Window;
import com.seleniti.enpareja.web.desktop.client.ui.widget.InfoPopupWidget;


public class MessageBoxFactoryImpl implements MessageBoxFactory {

	private static MessageBoxFactoryImpl instance;

	public static MessageBoxFactoryImpl get() {
		if (instance == null) {
			instance = new MessageBoxFactoryImpl();
		}

		return instance;
	}

	private MessageBoxFactoryImpl() {
		// Singleton
	}

	@Override
	public void info(String caption, String message, Object... messageParams) {
		InfoPopupWidget ipw = new InfoPopupWidget(caption, InfoType.INFO);
		ipw.setMessage(Format.substitute(message, messageParams));
		ipw.center();
	}

	@Override
	public void confirm(String caption, String message, ConfirmResponse confirmResponse,
			Object... messageParams) {
		boolean confirmed = Window.confirm(Format.substitute(message, messageParams));
		confirmResponse.onUserResponse(confirmed);
	}

	@Override
	public void prompt(String caption, String message, String initialValue, PromptResponse promptResponse,
			Object... messageParams) {
		if (initialValue == null) {
			initialValue = "";
		}

		String response = Window.prompt(Format.substitute(message, messageParams), initialValue);
		promptResponse.onUserResponse(response != null, response);
	}

	@Override
	public void alert(String caption, String message, Object... messageParams) {
		InfoPopupWidget ipw = new InfoPopupWidget(caption, InfoType.WARNING);
		ipw.setMessage(Format.substitute(message, messageParams));
		ipw.center();
	}

	@Override
	public void error(String caption, String message, Object... messageParams) {
		InfoPopupWidget ipw = new InfoPopupWidget(caption, InfoType.ERROR);
		ipw.setMessage(Format.substitute(message, messageParams));
		ipw.center();
	}

}
